package com.wondertek.poms.service.controller;

import com.wondertek.poms.common.vo.PageBean;
import com.wondertek.poms.common.vo.ResultBean;
import com.wondertek.poms.core.service.IAlbumContService;
import com.wondertek.poms.core.service.IPrdContService;
import com.wondertek.poms.dao.po.Image;
import com.wondertek.poms.dao.po.es.ContentVo;
import com.wondertek.poms.dao.pojo.AlbumContPojo;
import com.wondertek.poms.dao.pojo.PrdContSelPojo;
import com.wondertek.poms.dao.vo.PrdContVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


/**
 * @author gaozhengxing gaozhenghang@wondertek.com.cn
 * @date 2021/1/4 14:30
 * @description TODO
 */
@Api(value = "AlbumContController", description = "专辑")
@RestController
@RequestMapping(value="/album-cont")
@Slf4j
public class AlbumContController {

    @Autowired
    private IAlbumContService albumContService;
    @Autowired
    private IPrdContService prdContService;

    @GetMapping(value = "/get")
    public ResultBean getAlbumContList(String prdContId) {
        List<ContentVo> list=new ArrayList<>();
        try {
            list=albumContService.getAlbumContList(Long.parseLong(prdContId));
        }catch (Exception e){
            log.error(e.toString());
            return ResultBean.error(e.toString());
        }
        return ResultBean.ok(list);
    }

    /**
     * 节目列表查询
     *
     * @param prdCont 查询参数接收
     * @param page            页数
     * @param rows            记录数
     * @param sord            排序方式
     * @param sidx            排序列
     * @return 分页后的规则列表
     */
    @ApiOperation(value = "节目列表", notes = "节目列表")
    @GetMapping("/list-son")
    @ResponseBody
    public ResultBean list(
            PrdContSelPojo prdCont,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int rows,
            @RequestParam(defaultValue = "desc") String sord,
            @RequestParam(defaultValue = "c.CREATE_TIME") String sidx) {
        try {
            Page<PrdContVo> pages = prdContService.findAll(prdCont,page,rows,sord,sidx);
            if(pages!=null){
                return PageBean.ok(pages.getTotalPages(), pages.getTotalElements(), pages.getContent());
            }
        }catch (Exception e){
            ResultBean.error(e.toString());
        }
        return PageBean.ok(0, 0L, new ArrayList<>());
    }

    @GetMapping(value = "/image")
    public ResultBean getImageByNodeId(String nodeId,String resType) {
        List<Image> list=new ArrayList<>();
        try {
            list=albumContService.getImageByNodeId(nodeId, resType);
        }catch (Exception e){
            log.error(e.toString());
            return ResultBean.error(e.toString());
        }
        return ResultBean.ok(list);
    }

    @PostMapping(value = "/sava")
    public ResultBean savaAlbumCont(@RequestBody AlbumContPojo albumContPojo,HttpServletRequest req) {
        return albumContService.savaAlbumCont(albumContPojo);
    }

}
